﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
using System.Xml;
using System.Xml.Serialization;
using CPPEI.Coolzon.Common;

namespace CPPEI.Coolzon.Metadata.ParserV1
{
    public class DataSourceXmlParser : IDataSourceXmlParser
    {
        public DataSource Parse(XmlNode xDataSource)
        {
            try
            {
                if (xDataSource == null) return null;

                DataSource result = new DataSource
                {
                    RuntimeVersion = Version.Parse(xDataSource.GetXAttributeValue("RuntimeVersion")),
                    Version = Version.Parse(xDataSource.GetXAttributeValue("Version")),
                    Name = xDataSource.GetXAttributeValue("Name"),
                    Type = xDataSource.GetXAttributeValue("Type"),
                    Caption = xDataSource.GetXAttributeValue("Caption"),
                    Address = xDataSource.GetXElementValue("Address"),
                    CacheMode = TryParse(xDataSource.GetXAttributeValue("CacheMode"))
                };

                XmlNode xAuthentication = xDataSource.SelectSingleNode("Authentication");
                if (xAuthentication != null)
                {
                    result.Authentication = new Authentication()
                    {
                        User = xAuthentication.GetXAttributeValue("User"),
                        Mode = xAuthentication.GetXAttributeValue("Mode"),
                        Password = xAuthentication.GetXAttributeValue("Password")
                    };
                }
                XmlNode xDataSetList = xDataSource.SelectSingleNode("DataSet-List");
                if (xDataSetList != null)
                {
                    XmlNodeList xNodeList = xDataSetList.SelectNodes("DataSet");

                    List<DataSet> dataSetList = new List<DataSet>();
                    foreach (XmlNode xNode in xNodeList)
                    {
                        DataSet dataSet = new DataSet();
                        dataSet.Name = xNode.GetXAttributeValue("Name");
                        dataSet.Type = xNode.GetXAttributeValue("Type");
                        dataSet.Caption = xNode.GetXAttributeValue("Caption");
                        dataSet.ValueString = xNode.SelectSingleNode("ValueString").InnerText;
                        dataSet.CacheMode = TryParse(xNode.GetXAttributeValue("CacheMode"));

                        List<Parameter> parameters = new List<Parameter>();

                        XmlNode xNodeParameters = xNode.SelectSingleNode("Parameter-List");
                        XmlNodeList xParameterList = xNodeParameters.SelectNodes("Parameter");

                        foreach (XmlNode xNodeParameter in xParameterList)
                        {
                            Parameter parameter = new Parameter();
                            parameter.Name = xNodeParameter.GetXAttributeValue("Name");
                            parameter.Type = xNodeParameter.GetXAttributeValue("Type");
                            parameter.Caption = xNodeParameter.GetXAttributeValue("Caption");
                            parameter.ValueString = xNodeParameter.GetXAttributeValue("ValueString");
                            parameter.ValueType = xNodeParameter.GetXAttributeValue("ValueType");

                            parameters.Add(parameter);
                        }
                        dataSet.Parameters = parameters;
                        dataSetList.Add(dataSet);
                    }

                    result.DataSets = dataSetList;
                }
                return result;
            }
            catch (Exception ex)
            {
                Logger.ErrorToTag(this.GetType().ToString(), "{0}\n{1}", "DataSource序列化失败", ex.StackTrace);
                return null;
            }
        }

        public CacheMode TryParse(string cacheModeString)
        {
            CacheMode result;
            if (!Enum.TryParse<CacheMode>(cacheModeString, out result))
            {
                result = CacheMode.None;
            }
            return result;
        }
    }
}
